package com.yanqu.road.dataHelper.action;

import com.yanqu.road.dataHelper.action.excel.UserOnlineTimeDetail;
import com.yanqu.road.dataHelper.action.excel.ZhuanyuanBug;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.servercenter.ServerInfo;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class UserOnlineTimeExcel extends actionBase {
    public void go(List<Long> serverList) throws IOException {
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        List<Future> futureList = new ArrayList<>();
        int size = serverList.size() / 20;
        List<Long> tempServerList = new ArrayList<>();
        for (int i = 0; i < serverList.size(); i++) {
            tempServerList.add(serverList.get(i));
            if (tempServerList.size() >= size) {
                long finalI = tempServerList.get(0);
                List<Long> finalTempServerList = tempServerList;
                Future<Boolean> submit = executorService.submit(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        FileWriter fileWriter = new FileWriter("online_"+ finalI +".txt");
                        for (Long serverId : finalTempServerList) {
                            if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                                GameServer testGameServer = new GameServer(serverInfo);
                                UserOnlineTimeDetail ccc = new UserOnlineTimeDetail(testGameServer);
                                ccc.go(fileWriter);
                            }
                        }
                        fileWriter.flush();
                        fileWriter.close();
                        return true;
                    }
                });
                futureList.add(submit);
                tempServerList = new ArrayList<>();
            }
        }
        if (tempServerList.size() > 0) {
            long finalI = tempServerList.get(0);
            List<Long> finalTempServerList = tempServerList;
            Future<Boolean> submit = executorService.submit(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    FileWriter fileWriter = new FileWriter("online_"+ finalI +".txt");
                    for (Long serverId : finalTempServerList) {
                        if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                            ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                            GameServer testGameServer = new GameServer(serverInfo);
                            UserOnlineTimeDetail ccc = new UserOnlineTimeDetail(testGameServer);
                            ccc.go(fileWriter);
                        }
                    }
                    fileWriter.flush();
                    fileWriter.close();
                    return true;
                }
            });
            futureList.add(submit);
            tempServerList = new ArrayList<>();
        }
        isResult(futureList);
        DataMgr.makeExcelData2Txt("");
        DataMgr.outText("111");
    }
}
